<?php

class Validate {

	public function StripSome($parameter) {

		$parameter = strip_tags($parameter, '<br><b><u><i><h2>');

		return $parameter;

	}

	static public function StripAll($parameter) {

		$parameter = strip_tags($parameter);

		return $parameter;
	}

	static public function ValidatePassword($password) {

		if (preg_match("/\s+/", $password)) {
			return false;
		}

		if (preg_match("/^.*(?=.{6,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$/", $password)) {
			return true;
		} else {
			return false;
		}

	}

	static public function ValidateEmail($email) {

		$regexp = "/^[^0-9][A-z0-9_]+([.][A-z0-9_]+)*[@][A-z0-9_]+([.][A-z0-9_]+)*[.][A-z]{2,7}$/";

		if (preg_match($regexp, $email)) {
			return true;
		} else {
			return false;
		}
	}

	public function ValidateNumber($number) {

		if (is_int($number) == True) {
			return true;

		}
		return false;

	}

	public function ValidateDate($date) {
		$date = $this -> numericFromString($date);

		if (strlen($date) == 8) {
			$date = substr($date, -6);

		}

		if (strlen($date) == 6) {

			$y = substr($date, 0, -4);
			$d1 = substr($date, -2);
			$m = substr($date, 2, -2);

			if (checkdate($m, $d1, $y) == TRUE) {
				return true;

			} else {

				return false;
			}
		}
	}

	public function ValidatePRNMR($param1) {
		$personnummer = $this -> numericFromString($param1);

		if (strlen($personnummer) == 12) {
			$personnummer = substr($personnummer, -10);

		}

		if (strlen($personnummer) == 10) {
			if (substr($personnummer, 2, -6) > 12) {
				return false;
			}

			if (substr($personnummer, 4, -4) > 31) {
				return false;
			}

			if ($this -> luhn($personnummer) == TRUE) {

				return true;
			} else {

				return false;
			}

		} else {
			return false;

		}

	}

	function numericFromString($string) {
		return preg_replace('/[^0-9]/', '', $string);
	}

	function luhn($ssn) {
		$sum = 0;

		for ($i = 0; $i < strlen($ssn) - 1; $i++) {
			$tmp = substr($ssn, $i, 1) * (2 - ($i & 1));
			//växla mellan 212121212
			if ($tmp > 9)
				$tmp -= 9;
			$sum += $tmp;
		}

		//extrahera en-talet
		$sum = (10 - ($sum % 10)) % 10;

		return substr($ssn, -1, 1) == $sum;
	}

	// Test all the functions in the class.
	function Test() {
		// Date
		if ($this -> ValidateDate("1999-09-19") == false) {
			Log::LogMessage("Date test1 = fail");
			return false;
		}

		if ($this -> ValidateDate("1996-24-60") == true) {
			Log::LogMessage("Date test2 = fail");
			return false;
		}

		if ($this -> ValidateDate("1996-02-29") == false) {
			Log::LogMessage("Date test3 = fail");
			return false;
		}

		// Personnummer
		// Fyll I personnummer innan test.
		
		// Korrekt personnummer
		if ($this -> ValidatePRNMR("1981-12-18-9876") == false) {
			Log::LogMessage("Personnumer test1 = fail");
			return false;
		}

		// Felaktigt personnummer
		if ($this -> ValidatePRNMR("1981-12-18-9825") == true) {
			Log::LogMessage("Personnummer test2 = fail");
			return false;
		}

		// Korrekt personnummer (annat format)
		if ($this -> ValidatePRNMR("81-12-18-9876") == false) {
			Log::LogMessage("Personnummer test3 = fail");
			return false;
		}
		
		// Felaktig månad
		if ($this -> ValidatePRNMR("1981-13-18-9876") == true) {
			Log::LogMessage("Personnummer test4 = fail");
			return false;
		}

		// Felaktig dag
		if ($this -> ValidatePRNMR("1981-12-32-9876") == true) {
			Log::LogMessage("Personnummer test6 = fail");
			return false;
		}

		// Email
		if ($this -> ValidateEmail("abc@test.com") == false) {
			Log::LogMessage("Mail test1 = fail");
			return false;
		}

		if ($this -> ValidateEmail("abc@test.lnu.se") == false) {
			Log::LogMessage("Mail test2 = fail");
			return false;
		}

		if ($this -> ValidateEmail("abc@test.com125") == true) {
			Log::LogMessage("Mail test3 = fail");
			return false;
		}

		if ($this -> ValidateEmail("abc@test.comasgag") == true) {
			Log::LogMessage("Mail test4 = fail");
			return false;
		}

		if ($this -> ValidateEmail("abctest.com") == true) {
			Log::LogMessage("Mail test5 = fail");
			return false;
		}

		if ($this -> ValidateEmail("abc@test@fail.com") == true) {
			Log::LogMessage("Mail test6 = fail");
			return false;
		}

		// Teststräng - All
		if (Validate::StripAll("<body><p>Teststräng</p></body>") != "Teststräng") {
			Log::LogMessage("Strip-All test1 = fail");
			return false;
		}

		if (Validate::StripAll("<script>Teststräng här</script>") != "Teststräng här") {
			Log::LogMessage("Strip-All test1 = fail");
			return false;
		}

		// Teststräng - Some
		if ($this -> StripSome("<script>Teststräng här</script>") != "Teststräng här") {
			Log::LogMessage("Strip-Some test1 = fail");
			return false;
		}

		if ($this -> StripSome("<b>bold") != "<b>bold") {
			Log::LogMessage("Strip-Some test2 = fail");
			return false;
		}

		if ($this -> StripSome("<b><p>bold</p></b>") != "<b>bold</b>") {
			Log::LogMessage("Strip-Some test3 = fail");
			return false;
		}

		// Lösenord
		if (Validate::ValidatePassword("fsaasSAFAF124") == false) {
			Log::LogMessage("Password test1 = fail");
			return false;
		}

		if (Validate::ValidatePassword("2sS5g") == true) {
			Log::LogMessage("Password test2 = fail");
			return false;
		}

		if (Validate::ValidatePassword("safs SAS 512") == true) {
			Log::LogMessage("Password test3 = fail");
			return false;
		}

		if (Validate::ValidatePassword("asdasd") == true) {
			Log::LogMessage("Password test4 = fail");
			return false;
		}

		if (Validate::ValidatePassword("SAFASF") == true) {
			Log::LogMessage("Password test5 = fail");
			return false;
		}

		if (Validate::ValidatePassword("12515") == true) {
			Log::LogMessage("Password test6 = fail");
			return false;
		}

		// Nummer
		if ($this -> ValidateNumber(122) == false) {
			Log::LogMessage("Nummer test1 = fail");
			return false;
		}

		if ($this -> ValidateNumber("122asf") == true) {
			Log::LogMessage("Nummer test2 = fail");
			return false;
		}

		return true;
	}

}
